import WeUnknownDataProvider from '../../we-api/we-layer/layer/WeUnknownDataProvider'
import { We_getLowerCaseFileExtension } from '@/we-api/we-utils'
import { We_loadCloud, We_loadWind } from '@/we-api/we-layer/wrapper'
import WeBaseDisplay from '@/we-api/we-layer/wrapper/WeBaseDisplay'
import { We_Legends } from '@/we-api/we-layer/enum-legend'
import {
  We_layerShowExtrusion,
  We_layerShowIsoSurface,
  We_layerShowMovableWind,
} from '@/we-api/we-layer/wrapper/build'
import { We_setTimeRange } from '@/we-api/we-layer'

export const loadDrapLayer = (file) => {
  const unknownDataProvider = new WeUnknownDataProvider({
    url: file,
    getUrlCallback: (file: string) => `file:///${file}`,
  })

  if (file instanceof Array) {
    unknownDataProvider.requestFunc = () => {
      return Promise.resolve(file)
    }
  } else {
    const ext = We_getLowerCaseFileExtension(file)
    if (ext === '') {
      unknownDataProvider.requestFunc = () => {
        const nodeApi = window.api
        return Promise.resolve(nodeApi.listFiles([file]))
      }
    } else {
      unknownDataProvider.ext = ext
      unknownDataProvider.requestFunc = () => {
        const url = unknownDataProvider.getUrlCallback(file)
        return Cesium.Resource.fetchJson(url)
      }
    }
  }

  unknownDataProvider.touchContent()?.then(() => {
    const ext = unknownDataProvider.ext
    if (ext === 'awx') {
      We_loadCloud(
        new WeBaseDisplay({
          dataProvider: unknownDataProvider,
          display: {
            valueAndColorRamp: We_Legends.Cloud_Vis,
            valueRange: [0, 1.0],
          },
        }),
      )?.then((layer) => {
        if (!layer) return
        We_layerShowExtrusion(layer, true, {
          // numLon: Math.floor(3251 / 2),
          // numLat: Math.floor(3501 / 2),
          numLon: Math.floor(1024 * 1),
          numLat: Math.floor(1024 * 1),
          translucent: true,
          enableLight: false,
          enableNormalMap: true,
          alphaFactor: 4,
          extrusionScale: 1e4,
          normalFactor: 1 * 80.0,
        })
        We_setTimeRange(layer)
      })
    } else if (ext === 'json') {
      if (unknownDataProvider.isUV) {
        We_loadWind(
          new WeBaseDisplay({
            dataProvider: unknownDataProvider,
            display: {
              valueAndColorRamp: We_Legends.Wind_40,
            },
          }),
        )?.then((layer) => {
          if (!layer) return
          layer.show = true

          const f = Math.max(layer.rectangle.width, layer.rectangle.height)

          const windFactor = f < 1 ? f : 1

          const obj: any = We_layerShowIsoSurface(layer, true, {
            verbose: false,
            isoValues: [15, 20],
          })
          obj.scaleZ = 3 * windFactor

          We_layerShowMovableWind(layer, true, {
            numLon: Math.floor(256 * windFactor),
            numLat: Math.floor(256 * windFactor),
            drop_rate: 0.01,
            pointSize: 6,
            speed_factor: 1.5 * windFactor,
            colorize: true,
            showLayer: true,
            showSymbol: true,
            extrusion: 300000 * windFactor,
          })
        })
      }
    }
  })
}
